import 'package:dio/dio.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/main/login/http_const.dart';
import 'package:share_fit/app/controllers/main/login/reset/reset_state.dart';
import 'package:share_fit/app/network/net_tool.dart';

class ResetLogic extends BaseController {

  final state = ResetState();

  void setCodeText(value) {
    state.codeText = value;
    update(['sms_code_text']);
  }

  void setNameFocus(value) {
    state.isFocusedName = value;
    update(['reset_name_focus']);
  }

  void setCodeFocus(value) {
    state.isFocusedCode = value;
    update(['reset_code_focus']);
  }

  void setPwdFocus(value) {
    state.isFocusedPwd = value;
    update(['reset_pwd_focus']);
  }

  void setPwd2Focus(value) {
    state.isFocusedPwd2 = value;
    update(['reset_pwd2_focus']);
  }

  void setObscureText(value) {
    state.obscureText = value;
    update(['reset_pwd_focus','reset_pwd2_focus']);
  }

  @override
  String getRemotePath() {
    return '';
  }

  @override
  void initState() {
    // TODO: implement initState
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }

  Future<dynamic> sendVerifyCode(String userName, String verifyType) async {
    String params = '{"user_name": "$userName", "verify_type":"$verifyType", "app_id":"9sdaump8"}';
    Map<String, dynamic> header = {"language":"en"};
    final dio = Dio();
    // dio.options.contentType='application/x-www-form-urlencoded,application/json';
    dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true));
    dio.options.headers.addAll(header);
    try{
      Response response = await dio.post(HttpConst.sendVerifyCode, data: params,);
      return response;
    } catch (e) {
      if(e is DioException && e.response!=null) {
        return e.response!;
      }
      return '';
    }
  }

  Future<dynamic> forgetPwd(String userName, String verifyCode, String newPasswd) async {
    String params = '{"user_name": "$userName", "verify_code":"$verifyCode", "newPasswd":"$newPasswd", "checkPasswd":"$newPasswd"}';

    Map<String, dynamic> header = {"x-user-name":userName, "x-user-id":""};
    final dio = Dio();
    // dio.options.contentType='application/x-www-form-urlencoded,application/json';
    dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true));
    dio.options.headers.addAll(NetTool.getHeaders(extras: header));
    try{
      Response response = await dio.post(HttpConst.forgetPwd, data: params,);
      return response;
    } catch (e) {
      if(e is DioException && e.response!=null) {
        return e.response!;
      }
      return '';
    }
  }

}